/**
 * android3D的构造函数，设置缩放比，中心点坐标，上方向，前方向。
 */
var android3D = function(X_SCALE, center_vertice, atUp, atFront, gl) {
  this.X_SCALE = X_SCALE;
  this.center_vertice = center_vertice;
  this.atUp = atUp;
  this.atFront = atFront;

  this.colorEye = new Array(0, 1, 0);                                 //绿
  this.colorHead = new Array(0xEA / 0xFF, 0x43 / 0xFF, 0x35 / 0xFF);  //红
  this.colorBody = new Array(251 / 0xFF, 188 / 0xFF, 5 / 0xFF);       //黄
  this.colorArm = new Array(52 / 0xFF, 168 / 0xFF, 83 / 0xFF);        //绿
  this.colorLeg = new Array(66 / 0xFF, 133 / 0xFF, 244 / 0xFF);       //蓝

  this.android_head = new Draw3D(this.atEye, gl);

  this.android_eyer = new Draw3D(this.atEye, gl);
  this.android_eyel = new Draw3D(this.atEye, gl);

  this.android_body = new Draw3D(this.atEye, gl);

  this.android_armr = new Draw3D(this.atEye, gl);
  this.android_arml = new Draw3D(this.atEye, gl);

  this.android_legr = new Draw3D(this.atEye, gl);
  this.android_legl = new Draw3D(this.atEye, gl);
};

android3D.prototype.test = function() {
  console.log(this.tttt);
};

android3D.prototype.tttt = 'ttttt';


android3D.prototype.X_SCALE = 1;  //缩放系数，适应画布的变化
android3D.prototype.center_vertice =
    new Array(0, 0, 0, 1);                         //机器人中心点的位置
android3D.prototype.atUp = new Array(0, 1, 0);     //机器人上方向
android3D.prototype.atFront = new Array(0, 0, 1);  //机器人的前方向

android3D.prototype.atEye = new Array(0, 2, 20);
android3D.prototype.setAtEye = function(atEYEX, atEYEY, atEYEZ) {
  this.atEye[0] = atEYEX;
  this.atEye[1] = atEYEY;
  this.atEye[2] = atEYEZ;
};


/**
 * 身体各部位的颜色
 */
android3D.prototype.colorEye = new Array(0, 1, 0);  //黑
android3D.prototype.colorHead =
    new Array(0xEA / 0xFF, 0x43 / 0xFF, 0x35 / 0xFF);  //红
android3D.prototype.colorBody =
    new Array(251 / 0xFF, 188 / 0xFF, 5 / 0xFF);  //黄
android3D.prototype.colorArm =
    new Array(52 / 0xFF, 168 / 0xFF, 83 / 0xFF);  //绿
android3D.prototype.colorLeg =
    new Array(66 / 0xFF, 133 / 0xFF, 244 / 0xFF);  //蓝

android3D.prototype.setEyeColor = function(r, g, b) {
  this.colorEye[0] = r;
  this.colorEye[1] = g;
  this.colorEye[2] = b;
};
android3D.prototype.setHeadColor = function(r, g, b) {
  this.colorHead[0] = r;
  this.colorHead[1] = g;
  this.colorHead[2] = b;
};
android3D.prototype.setBodyColor = function(r, g, b) {
  this.colorBody[0] = r;
  this.colorBody[1] = g;
  this.colorBody[2] = b;
};
android3D.prototype.setArmColor = function(r, g, b) {
  this.colorArm[0] = r;
  this.colorArm[1] = g;
  this.colorArm[2] = b;
};
android3D.prototype.setLegColor = function(r, g, b) {
  this.colorLeg[0] = r;
  this.colorLeg[1] = g;
  this.colorLeg[2] = b;
};



/**
 * 键盘监听函数
 * 方向键上：向前走
 * 方向键下：向后走
 * 方向键右：向右转一定角度
 * 方向键左：向左转动一定角度
 * 数字键1：右胳膊向上转
 * 数字键2：右胳膊向下转
 * 数字键3：左胳膊向上转
 * 数字键4：左胳膊向下转
 */
android3D.prototype.rotate_key = 0;  //键盘改变身体的朝向的角度
android3D.prototype.key_armL_rotateA = 0;  //键盘改变的左胳膊的角度
android3D.prototype.key_armR_rotateA = 0;  //键盘改变的右胳膊的角度

android3D.prototype.keydown = function(ev) {
  if (ev.keyCode == 37) {  // left
    this.rotate_key += Math.PI / 30;
  } else if (ev.keyCode == 39) {  // right
    this.rotate_key -= Math.PI / 30;

  } else if (ev.keyCode == 38) {  // up
    this.center_vertice[0] += Math.sin(this.rotate_key);
    this.center_vertice[2] += Math.cos(this.rotate_key);

  } else if (ev.keyCode == 40) {  // down
    this.center_vertice[0] -= Math.sin(this.rotate_key);
    this.center_vertice[2] -= Math.cos(this.rotate_key);

  } else if (ev.keyCode == 49) {  // 1
    this.key_armR_rotateA += Math.PI / 30;
  } else if (ev.keyCode == 50) {  // 2
    this.key_armR_rotateA -= Math.PI / 30;

  } else if (ev.keyCode == 51) {  // 3
    this.key_armL_rotateA += Math.PI / 30;

  } else if (ev.keyCode == 52) {  // 4
    this.key_armL_rotateA -= Math.PI / 30;

  } else {
    console.log('无效的按键');
    return;
  }
  this.setArmL(this.key_armL_rotateA);
  this.setArmR(this.key_armR_rotateA);
  this.bodyRotate(this.rotate_key);
  // gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  // this.drawAndroid3D(gl);
};

android3D.prototype.rotateMe = function(gl) {
  var i = 0;
  var tick = function() {
    var a = i * Math.PI / 30;
    i++;
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    this.drawAndroid3D(
        [0, 0, 0, 1], [0, 1, 0], new Array(Math.sin(a), 0, Math.cos(a)), gl);
    requestAnimationFrame(tick);  // Request that the browser ?calls tick
  };
  tick();
};

android3D.prototype.rotateEye = function(gl) {
  var i = 0;
  var tick = function() {
    var a = i * Math.PI / 30;
    i++;
    setEye(20 * Math.sin(a), 2, 20 * Math.cos(a));
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    this.drawAndroid3D([0, 0, 0, 1], [0, 1, 0], [0, 0, 1], gl);
    requestAnimationFrame(tick);  // Request that the browser ?calls tick
  };
  tick();
};


/**
 * 身体的转动
 */
android3D.prototype.bodyRotate = function(a) {
  this.atFront[0] = Math.sin(a);
  this.atFront[2] = Math.cos(a);
};


/**
 * 胳膊的转动
 */
android3D.prototype.ARMLY = 0;
android3D.prototype.ARMLZ = 0;
android3D.prototype.ARMRY = 0;
android3D.prototype.ARMRZ = 0;

android3D.prototype.setArmL = function(rotateA) {
  this.ARMLY = 1.9 * (1 - Math.cos(rotateA));
  this.ARMLZ = 1.9 * Math.sin(rotateA);
};

android3D.prototype.setArmR = function(rotateA) {
  this.ARMRY = 1.9 * (1 - Math.cos(rotateA));
  this.ARMRZ = 1.9 * Math.sin(rotateA);
};

android3D.prototype.android_head;

android3D.prototype.android_eyer;
android3D.prototype.android_eyel;

android3D.prototype.android_body;

android3D.prototype.android_armr;
android3D.prototype.android_arml;

android3D.prototype.android_legr;
android3D.prototype.android_legl;

/**
 * 画出安卓机器人
 */
android3D.prototype.drawAndroid3D = function() {


  /**
   * bodyH 身体高度
   * armH 胳膊高
   * legH 腿高
   */
  var bodyH = 3, armH = 1.9, legH = 1.2;
  /**
   * bodyR 身体半径
   * alR 腿和胳膊的半径
   */
  var bodyR = 1.5, alR = 0.3, EYE_R = 0.1;


  this.calMatrix();


  var body1 = new Array(0.0, bodyH / 2, 0.0, 1.0);
  var body2 = new Array(0.0, (-1) * bodyH / 2, 0.0, 1.0);
  var armL1 = new Array(1.9, (bodyH / 2) - 0.3, 0.0, 1.0);
  var armL2 =
      new Array(1.9, (bodyH / 2) - 0.3 - armH + this.ARMLY, this.ARMLZ, 1.0);
  var armR1 = new Array(-1.9, (bodyH / 2) - 0.3, 0.0, 1.0);
  var armR2 =
      new Array(-1.9, (bodyH / 2) - 0.3 - armH + this.ARMRY, this.ARMRZ, 1.0);
  var legL1 = new Array(0.6, (-1) * bodyH / 2, 0.0, 1.0);
  var legL2 = new Array(0.6, (-1) * bodyH / 2 - legH, 0.0, 1.0);
  var legR1 = new Array(-0.6, (-1) * bodyH / 2, 0.0, 1.0);
  var legR2 = new Array(-0.6, (-1) * bodyH / 2 - legH, 0.0, 1.0);
  var head = new Array(0.0, bodyH / 2 + 0.1, 0.0, 1.0);


  var EYE_A1 = Math.PI / 8;
  var EYE_A2 = Math.PI / 8;
  var eyeY = bodyR * Math.sin(EYE_A1);
  var eyeX = bodyR * Math.cos(EYE_A1) * Math.sin(EYE_A2);
  var eyeZ = bodyR * Math.cos(EYE_A1) * Math.cos(EYE_A2);

  var eyeL = new Array(head[0] + eyeX, head[1] + eyeY, head[2] + eyeZ, 1.0);
  var eyeR = new Array(head[0] - eyeX, head[1] + eyeY, head[2] + eyeZ, 1.0);



  this.android_head.drawBall(head, [0, 1, 0], 0.5, bodyR, this.colorHead);
  this.android_eyer.drawBall(eyeL, [0, 1, 0], 1, EYE_R, this.colorEye);
  this.android_eyel.drawBall(eyeR, [0, 1, 0], 1, EYE_R, this.colorEye);

  this.android_body.drawCylinder(body1, body2, bodyR, this.colorBody);


  this.android_armr.drawCylinder(armL1, armL2, alR, this.colorArm);
  this.android_arml.drawCylinder(armR1, armR2, alR, this.colorArm);

  this.android_legr.drawCylinder(legL1, legL2, alR, this.colorLeg);
  this.android_legl.drawCylinder(legR1, legR2, alR, this.colorLeg);
};

android3D.prototype.updateAndroid3D = function() {
  /**
*bodyH 身体高度
*armH 胳膊高
*legH 腿高
*/
  var bodyH = 3, armH = 1.9, legH = 1.2;
  /**
   * bodyR 身体半径
   * alR 腿和胳膊的半径
   */
  var bodyR = 1.5, alR = 0.3, EYE_R = 0.1;


  this.calMatrix();


  var body1 = new Array(0.0, bodyH / 2, 0.0, 1.0);
  var body2 = new Array(0.0, (-1) * bodyH / 2, 0.0, 1.0);
  var armL1 = new Array(1.9, (bodyH / 2) - 0.3, 0.0, 1.0);
  var armL2 =
      new Array(1.9, (bodyH / 2) - 0.3 - armH + this.ARMLY, this.ARMLZ, 1.0);
  var armR1 = new Array(-1.9, (bodyH / 2) - 0.3, 0.0, 1.0);
  var armR2 =
      new Array(-1.9, (bodyH / 2) - 0.3 - armH + this.ARMRY, this.ARMRZ, 1.0);
  var legL1 = new Array(0.6, (-1) * bodyH / 2, 0.0, 1.0);
  var legL2 = new Array(0.6, (-1) * bodyH / 2 - legH, 0.0, 1.0);
  var legR1 = new Array(-0.6, (-1) * bodyH / 2, 0.0, 1.0);
  var legR2 = new Array(-0.6, (-1) * bodyH / 2 - legH, 0.0, 1.0);
  var head = new Array(0.0, bodyH / 2 + 0.1, 0.0, 1.0);


  var EYE_A1 = Math.PI / 8;
  var EYE_A2 = Math.PI / 8;
  var eyeY = bodyR * Math.sin(EYE_A1);
  var eyeX = bodyR * Math.cos(EYE_A1) * Math.sin(EYE_A2);
  var eyeZ = bodyR * Math.cos(EYE_A1) * Math.cos(EYE_A2);

  var eyeL = new Array(head[0] + eyeX, head[1] + eyeY, head[2] + eyeZ, 1.0);
  var eyeR = new Array(head[0] - eyeX, head[1] + eyeY, head[2] + eyeZ, 1.0);



  this.android_head.updateBall(head, [0, 1, 0]);
  this.android_eyer.updateBall(eyeR, [0, 1, 0]);
  this.android_eyel.updateBall(eyeL, [0, 1, 0]);

  this.android_body.updateCylinder(body1, body2);

  this.android_armr.updateCylinder(armR1, armR2);
  this.android_arml.updateCylinder(armL1, armL2);

  this.android_legr.updateCylinder(legR1, legR2);
  this.android_legl.updateCylinder(legL1, legL2);
};
android3D.prototype.calMatrix = function() {
  var modelMatrix = new Matrix4();
  var a_zx;
  var a_z;
  ;
  if (this.atUp[0] == 0) {
    a_zx = Math.PI / 2;
    if (this.atUp[2] < 0) {
      a_zx = a_zx * (-1);
    }
  } else {
    if (this.atUp[2] == 0) {
      if (this.atUp[0] > 0) {
        a_zx = 0;
      } else {
        a_zx = Math.PI;
      }

    } else {
      a_zx = Math.atan(this.atUp[2] / this.atUp[0]);
      if (this.atUp[0] < 0) {
        a_zx += Math.PI;
      }
    }
  }

  var x_y_z = Math.sqrt(
      this.atUp[0] * this.atUp[0] + this.atUp[1] * this.atUp[1] +
      this.atUp[2] * this.atUp[2]);
  if (this.atUp[1] == 0) {
    a_z = Math.PI / 2;
  } else {
    a_z = Math.acos(this.atUp[1] / x_y_z);
  }


  var a_rY;
  if (this.atFront[2] == 0) {
    a_rY = Math.PI / 2;
    if (this.atFront[0] < 0) {
      a_rY *= -1;
    }
  } else {
    if (this.atFront[0] == 0) {
      if (this.atFront[2] < 0) {
        a_rY = Math.PI;
      } else {
        a_rY = 0;
      }
    } else {
      a_rY = Math.atan(this.atFront[0] / this.atFront[2]);
      if (this.atFront[2] < 0) {
        a_rY += Math.PI;
      }
    }
  }

  modelMatrix.setScale(this.X_SCALE, 1, this.X_SCALE);
  // modelMatrix.setTranslate(vertice[0], vertice[1], vertice[2]);
  modelMatrix.translate(
      this.center_vertice[0], this.center_vertice[1], this.center_vertice[2]);

  modelMatrix.rotate(180 * a_zx / Math.PI, 0, 1, 0);
  modelMatrix.rotate(180 * a_z / Math.PI, 0, 0, 1);
  modelMatrix.rotate((-1) * 180 * a_zx / Math.PI, 0, 1, 0);

  modelMatrix.rotate(180 * a_rY / Math.PI, 0, 1, 0);


  this.android_head.setModelMatrix(modelMatrix);
  this.android_eyer.setModelMatrix(modelMatrix);
  this.android_eyel.setModelMatrix(modelMatrix);

  this.android_body.setModelMatrix(modelMatrix);


  this.android_armr.setModelMatrix(modelMatrix);
  this.android_arml.setModelMatrix(modelMatrix);

  this.android_legr.setModelMatrix(modelMatrix);
  this.android_legl.setModelMatrix(modelMatrix);
};